#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
@File    : graph.py
@Time    : 2023/01/27 20:21
@Author  : 郭瑞强
@Contact : sunraing@126.com
@Version : 0.1
@License : BSD 3-Clause License
@Desc    : 图的实现
"""


from .vertex import Vertex


class Graph:
    def __init__(self) -> None:
        self.vert_dict: dict[type, Vertex] = dict()

    def add_vertex(self, key):
        # assert key not in self.vert_dict
        self.vert_dict[key] = Vertex(key)

    def get_vertex(self, key):
        return self.vert_dict.get(key, None)

    def add_edge(self, f, t, weight=0):
        if f not in self.vert_dict:
            self.add_vertex(f)
        if t not in self.vert_dict:
            self.add_vertex(t)

        self.vert_dict[f].add_neighbor(self.vert_dict[t], weight)

    def get_vertexes(self):
        return list(self.vert_dict.keys())

    def __contains__(self, key):
        return key in self.vert_dict

    def __iter__(self):
        return iter(self.vert_dict.values())

    def __len__(self):
        return len(self.vert_dict)
